﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebService_Newstore.DAO
{
    public class LgBpDAO:BaseDAO
    {
        public int id { get; set; }
        public int idBp { get; set; }
        public int numeroLigne { get; set; }                       // pour l'ordre d'affichage des lignes (ou ordre d'impression)
        public int idLgCmd { get; set; }
        public float qteAPrepa { get; set; }
        public float qtePreparee { get; set; }
        public int statut { get; set; }
        public string codeUnitePrepa { get; set; }                    // unité dans laquelle aura lieu la préparation
        public float qteReference { get; set; }                     // quantité de produit à préparer en unité de référence de l'article
        public int idEmplacement { get; set; }                      // emplacement sur lequel doit avoir lieu la préparation
        public int idAnomalie { get; set; }

        public LgBpDAO()
        {
            
        }

        public LgBpDAO(int id, int idBp, int numLg, float qteAPrepa, float qtePrep, int statut, string codeUnite, float qteRef, int idEmp, int idAn, int idLgCmd)
        {
            this.id = id;
            this.idBp = idBp;
            this.numeroLigne = numLg;
            this.qteAPrepa = qteAPrepa;
            this.qtePreparee = qtePrep;
            this.statut = statut;
            this.codeUnitePrepa = codeUnite;
            this.qteReference = qteRef;
            this.idEmplacement = idEmp;
            this.idAnomalie = idAn;
            this.idLgCmd = idLgCmd;
        }

        public LgBpDAO(int idBp, int numLg, float qteAPrepa, float qtePrep, int statut, string codeUnite, float qteRef, int idEmp, int idAn,int idLgCmd)
        {
            this.idBp = idBp;
            this.numeroLigne = numLg;
            this.qteAPrepa = qteAPrepa;
            this.qtePreparee = qtePrep;
            this.statut = statut;
            this.codeUnitePrepa = codeUnite;
            this.qteReference = qteRef;
            this.idEmplacement = idEmp;
            this.idAnomalie = idAn;
            this.idLgCmd = idLgCmd;
        }
        public LgBpDAO(int idLg)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var userQuery = from lg_bp in newStoreContext.lg_bp
                                    where lg_bp.id_lgBp == idLg
                                    select lg_bp;

                    if (userQuery.LongCount() == 0)
                    {
                        //On instancie un DAO vide pour faire remonter l'erreur au niveau du controller
                        this.id = 0;
                    }

                    foreach (var ligne in userQuery)
                    {
                        //On instancie la ligne de BP
                        this.id = ligne.id_lgBp;
                        this.idBp = (int)ligne.id_bp;
                        this.numeroLigne = (int)ligne.num_lg;
                        this.qteAPrepa = (float)ligne.qte_aprepa;
                        this.qtePreparee = (float)ligne.qte_prep;
                        this.statut = (int)ligne.statut;
                        this.codeUnitePrepa = (string)ligne.id_unite_prepa;
                        this.idEmplacement = (int)ligne.id_emp;
                        this.idAnomalie = (int)ligne.id_anomalie;
                        this.idLgCmd = (int)ligne.id_lgCmd;

                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                }
            }
        }

        // méthode Update
        public static LgBpDAO Update(LgBpDAO lgBp)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    lg_bp lig = newStoreContext.lg_bp.FirstOrDefault(i => i.id_lgBp == lgBp.id);
                    if (lig != null)
                    {
                        lig.qte_prep = lgBp.qtePreparee;
                        lig.id_anomalie = lgBp.idAnomalie;
                        lig.statut = lgBp.statut; 

                        newStoreContext.SaveChanges();
                        return lgBp;
                    }
                    else
                    {
                        //insertion
                        lg_bp lgToInsert = new lg_bp();
                        lgToInsert.id_bp = lgBp.idBp;
                        lgToInsert.num_lg = lgBp.numeroLigne;
                        lgToInsert.qte_aprepa = lgBp.qteAPrepa;
                        lgToInsert.qte_prep = lgBp.qtePreparee;
                        lgToInsert.statut = lgBp.statut;
                        lgToInsert.id_unite_prepa = lgBp.codeUnitePrepa;
                        lgToInsert.id_emp = lgBp.idEmplacement;
                        lgToInsert.id_anomalie = lgBp.idAnomalie;
                        lgToInsert.id_lgCmd = lgBp.idLgCmd;
                        newStoreContext.lg_bp.AddObject(lgToInsert);
                        newStoreContext.SaveChanges();
                        lgBp.id = lgToInsert.id_lgBp;
                        return lgBp;
                        
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                    lgBp = null;
                    return lgBp;
                }
            }
        }

        //lignes liées à une entete
        public static List<LgBpDAO> FindLgByBp(int idBp)
        {
            List<LgBpDAO> listeLg = new List<LgBpDAO>();

            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from lg_bp in newStoreContext.lg_bp
                                where lg_bp.id_bp == idBp
                                select lg_bp;

                    foreach (var lg in query)
                    {

                        //On instancie les lignes 
                        LgBpDAO lgBpToAdd       = new LgBpDAO();
                        lgBpToAdd.id            = lg.id_lgBp; 
                        lgBpToAdd.idBp          =  (int)lg.id_bp;
                        lgBpToAdd.idLgCmd       = (int)lg.id_lgCmd;
                        lgBpToAdd.numeroLigne   = (int)lg.num_lg;
                        lgBpToAdd.qteAPrepa     = (float)lg.qte_aprepa;
                        lgBpToAdd.qtePreparee   = (float)lg.qte_prep;
                        lgBpToAdd.statut        = (int)lg.statut; 
                        lgBpToAdd.codeUnitePrepa =   lg.id_unite_prepa;
                        lgBpToAdd.idEmplacement = (int)lg.id_emp;
                        lgBpToAdd.idAnomalie    = (int)lg.id_anomalie; 

                        listeLg.Add(lgBpToAdd);

                    }

                    return listeLg;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return listeLg;
                }

            }
        }

        //nombre de lignes non saisies pour un BP
        public static int CountNotReadyLines(int bpNo)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var query = from lg_bp in newStoreContext.lg_bp
                                where lg_bp.id_bp == bpNo && lg_bp.statut == 0
                                select lg_bp;

                    return query.Count();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.ReadLine();
                    return 0;
                }
            }
        }

        public static float getStockTheoric(int idEmplacement)
        {
            using (newStoreContext = new newStoreEntities())
            {
                try
                {
                    var stTh = (from lg_bp in newStoreContext.lg_bp
                                where lg_bp.id_emp == idEmplacement
                                select lg_bp).Sum(x => x.qte_aprepa);

                    return (float)stTh;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException);
                    return 0;
                }
            }
        }
    }
}
